package com.woniuxy.controller;

import com.woniuxy.dto.RecordDetailDTO;
import com.woniuxy.entity.ServiceRecord;
import com.woniuxy.service.RecordService;
import com.woniuxy.service.TagsResidentRelationService;
import com.woniuxy.util.ResponseUtil;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务记录控制器
 * 处理服务记录相关的HTTP请求
 */
@RestController
@RequestMapping("/service")
public class RecordController {
    @Resource
    private RecordService recordService;
    @Resource
    private TagsResidentRelationService relationService;
    
    /**
     * 根据状态查询服务记录
     * @param status 记录状态
     * @return 返回指定状态的记录列表
     */
    @GetMapping("/record")
    public ResponseUtil queryRecordByStatus(@RequestParam(required = false) Integer userId,
                                            @RequestParam(required = false) Integer status,
                                            @RequestParam(required = false) String name) throws ParseException {
        ResponseUtil result = recordService.queryRecordByStatus(status,userId,name);
        return result;
    }
    
    /**
     * 查询已完成的服务记录
     * @return 返回所有已完成的服务记录
     */
    @GetMapping
    public ResponseUtil queryRecordFinished(@RequestParam(required = false) Integer userId,
                                            @RequestParam(required = false) String name) throws ParseException {
        ResponseUtil result = recordService.queryRecordFinished(userId,name);
        return result;
    }
    
    /**
     * 根据记录ID查询详细信息
     * @param id 记录ID
     * @return 返回指定记录的详细信息
     */
    @GetMapping("/detail/{id}")
    public ResponseUtil queryDetail(@PathVariable("id")Integer id){
        ResponseUtil result = recordService.queryDetailByRecordId(id);
        return result;
    }
    
    /**
     * 更新服务记录
     * @param serviceRecord 服务记录对象
     * @return 返回更新操作的响应结果
     */
    @PutMapping
    public ResponseUtil update(@RequestBody ServiceRecord serviceRecord){
        ResponseUtil result = recordService.update(serviceRecord);
        return result;
    }
    
    /**
     * 更新记录详情
     * @param detail 记录详情对象
     * @return 返回更新操作的响应结果
     */
    @PutMapping("/detail")
    public ResponseUtil updateDetail(@RequestBody RecordDetailDTO detail){
        ResponseUtil result = recordService.updateDetail(detail);
        return result;
    }
    
    /**
     * 添加新的服务记录
     * @param record 服务记录对象
     * @return 返回添加操作的响应结果
     */
    @PostMapping
    public ResponseUtil add(@RequestBody ServiceRecord record){
        ResponseUtil result = recordService.add(record);
        return result;
    }
    
    /**
     * 根据居民ID查询所有记录（APP端使用）
     * @param residentId 居民ID
     * @return 返回指定居民的所有服务记录
     */
    @GetMapping("/resident/{residentId}")
    public ResponseUtil queryAllByResidentIdOnApp(@PathVariable Integer residentId){
        ResponseUtil result = recordService
                .queryAllByResidentIdOnApp(residentId);
        return result;
    }
    @GetMapping("/resident/name/{name}/status/{status}")
    public ResponseUtil queryByName(@PathVariable("status") Integer status,@PathVariable("name") String name){
        ResponseUtil result = recordService.queryByName(status,name);
        return result;
    }

}
